package com.cxxx.guns.modular.portal.controller;
//
//                       _oo0oo_
//                      o8888888o
//                      88" . "88
//                      (| -_- |)
//                      0\  =  /0
//                    ___/`---'\___
//                  .' \\|     |// '.
//                 / \\|||  :  |||// \
//                / _||||| -:- |||||- \
//               |   | \\\  -  /// |   |
//               | \_|  ''\---/''  |_/ |
//               \  .-\__  '-'  ___/-. /
//             ___'. .'  /--.--\  `. .'___
//          ."" '<  `.___\_<|>_/___.' >' "".
//         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//         \  \ `_.   \_ __\ /__ _/   .-` /  /
//     =====`-.____`.___ \_____/___.-`___.-'=====
//                       `=---='
//
//
//     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//               佛祖保佑         永无BUG
//

import com.baomidou.mybatisplus.plugins.Page;
import com.cxxx.guns.modular.backend.model.Announcement;
import com.cxxx.guns.modular.backend.model.Video;
import com.cxxx.guns.modular.backend.pojo.Pages;
import com.cxxx.guns.modular.backend.pojo.VideoPojo;
import com.cxxx.guns.modular.backend.service.IVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.List;

/**
 * @author: yyi
 * @date: 2018/9/18 17:11
 * @parameters:
 * @desc:
 **/
@Controller
@RequestMapping("/portal")
public class VideoController {
    @Autowired
    private IVideoService videoService;
    private static String PREDEX="/portal/";
    @RequestMapping("videolist/{currentPage}")
    public String video(@PathVariable("currentPage") int currentPage, Model model){
        /*分页大小写死*/
        Page<Video> page = new Page<>(currentPage,3);
        Page<Video> pages = videoService.selectPage(page);
        Pages pg = new Pages();
        pg.setRecords(pages.getRecords());
        pg.setCurrentPage(page.getCurrent());
        pg.setTotal(page.getPages());
        model.addAttribute("pg",pg);
        return PREDEX + "childhtml/other/download/video/starlist.html";
    }
    @RequestMapping(value = "/videoprevious/{page}")
    public String previous(@PathVariable("page") int currentPage,Model model){
        int page1 = currentPage-1;
        if (page1==0){
            Page<Video> page = new Page<>(currentPage,3);
            Page<Video> pages = videoService.selectPage(page);
            Pages pg = new Pages();
            pg.setRecords(pages.getRecords());
            pg.setCurrentPage(pages.getCurrent());
            pg.setTotal(pages.getPages());
            model.addAttribute("pg",pg);
            return PREDEX +"childhtml/other/download/video/starlist.html";
        }else{
            Page<Video> page = new Page<>(page1,3);
            Page<Video> pages = videoService.selectPage(page);
            Pages pg = new Pages();
            pg.setRecords(pages.getRecords());
            pg.setCurrentPage(pages.getCurrent());
            pg.setTotal(pages.getPages());
            model.addAttribute("pg",pg);
            return PREDEX +"childhtml/other/download/video/starlist.html";
        }
    }
    @RequestMapping(value = "/videonext/{total}/{page}")
    public String next(@PathVariable("page") int currentPage,@PathVariable("total") int total,Model model){
        int page1 = currentPage+1;
        if (page1<=total){
            Page<Video> page = new Page<>(page1,3);
            Page<Video> pages = videoService.selectPage(page);
            Pages pg = new Pages();
            pg.setRecords(pages.getRecords());
            pg.setCurrentPage(pages.getCurrent());
            pg.setTotal(pages.getPages());
            model.addAttribute("pg",pg);
            return PREDEX +"childhtml/other/download/video/starlist.html";
        }else{
            Page<Video> page = new Page<>(currentPage,3);
            Page<Video> pages = videoService.selectPage(page);
            Pages pg = new Pages();
            pg.setRecords(pages.getRecords());
            pg.setCurrentPage(pages.getCurrent());
            pg.setTotal(pages.getPages());
            model.addAttribute("pg",pg);
            return PREDEX +"childhtml/other/download/video/starlist.html";
        }
    }
    /*播放视频*/
    @RequestMapping("/startvideo/{id}")
    public String startVideo(@PathVariable("id") int id,Model model){
        Video video = videoService.selectById(id);
        Date date = video.getCreateDate();
        /*制度文件的id分类为8，直接穿进去8就可以*/
        Video next = videoService.nextVideo(date);
        Video previous = videoService.previousVideo(date);
        VideoPojo videoPojo = new VideoPojo();
        videoPojo.setVideo(video);
        if (next!=null){
            videoPojo.setNextItemHead(next.getVideoDescribe());
            videoPojo.setNextItemId(next.getId());
        }else{
            videoPojo.setNextItemHead("无");
            videoPojo.setNextItemId(video.getId());
        }
        if (previous!=null){
            videoPojo.setPreviousHead(previous.getVideoDescribe());
            videoPojo.setPreviousId(previous.getId());
        }else {
            videoPojo.setPreviousHead("无");
            videoPojo.setPreviousId(video.getId());
        }
        List<Video> list = videoService.listVideoSeven();
        model.addAttribute("video",videoPojo);
        model.addAttribute("list",list);
        return PREDEX +"childhtml/other/download/video/starmsg.html";
    }
}
